package com.android.replace;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

public class CStruct2Java {
    
    private static String CHeaderPath ;
    
    private static String readLine(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        try {
            line = br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return line;
    }
    
    private static void generateClass(String line){
        String[] split = line.split(" ");
        String className = split[1];
        System.out.println("public class "+className+"");
    }
    
    private static void generateContent(String line){
        if(line.contains("*")){
            generatePointer(line);
        }else if(line.contains("[")&&line.contains("]")){
            generateArray(line);
        }else{
            generateSimple(line);
        }
    }
    
    private static void generateSimple(String line){
        line = line.replaceAll(";", "").trim();
        String[] split = line.split(" ");
        String parameter = split[1];
        String type = split[0];
        if (line.contains("struct")) {
            split = line.split(" ");
            type = split[1];
            parameter = split[2];
        }
        System.out.println("private " + type + " " + parameter + ";");
    }
    
    private static void generatePointer(String line){
        line = line.replaceAll(";", "").trim();
        line = line.replaceAll("\\*", "").trim();
        String[] split = line.split(" ");
        String parameter = split[1];
        String type = split[0];
        if (line.contains("struct")) {
            split = line.split(" ");
            type = split[1];
            parameter = split[2];
        }
        if("char".equals(type)){
            type = "String";
        }
        System.out.println("private " + type + " " + parameter + ";");
    }
    
    private static void generateArray(String line){
        line = line.replaceAll(";", "").trim();
        String[] split = line.split(" ");
        String parameter = split[1];
        String type = split[0];
        int index = 0;
        if (line.contains("struct")) {
            split = line.split(" ");
            type = split[1];
            parameter = split[2];
            index = parameter.indexOf("[");
            parameter = parameter.substring(0, index);
        }else{
            index = parameter.indexOf("[");
            parameter = parameter.substring(0, index);
        }
        if("char".equals(type)){
            type = "String";
        }
        System.out.println("private " + "List<"+type+">" + " " + parameter + ";");
    }
    
    private static void generateEnd(String line){
        System.out.println("}");
    }
    
    private static void parseHeader(String file){
        if (TextUtils.isEmpty(file)) {
            return;
        }
        File destFile = new File(file + ".tmp");
        File sourceFile = new File(file);
        BufferedReader br = null;
        BufferedWriter bw = null;
        boolean success = false;
        try {
            bw = new BufferedWriter(new FileWriter(destFile));
            br = new BufferedReader(new FileReader(sourceFile));
            String line;
            boolean hasStruct = false;
            boolean beginProcess = false;
            while ((line = br.readLine()) != null) {
                if(line.contains("//*annotation")){
                    beginProcess = true;
                }else if(line.contains("struct")&&!(line.contains(";"))&&beginProcess){
                    hasStruct = true;
                    generateClass(line);
                }else if(line.contains("}")&&hasStruct){
                    hasStruct =false;
                    beginProcess = false;
                    generateEnd("");
                }else if(hasStruct){
                    generateContent(line);
                }
            }
            success = true;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            success = false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            success = false;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
//            if (success) {
//                sourceFile.delete();
//                destFile.renameTo(sourceFile);
//            }
        }

    }

    public static void main(String[] args) {
//        System.out.println("Please Input c header path!");
//        String headPath = readLine();
//        if(headPath==null){
//            System.out.println("Path is null,please input again!!!!");
//            headPath = readLine();
//        }
//        if(TextUtils.isEmpty(headPath)){
//            System.out.println("System exit!!!");
//            return;
//        }
        String headPath = "D:\\Development\\Android\\T58V30Git\\Test\\src\\Test.h";
        File headFile = new File(headPath);
        if(headFile.exists()&&headFile.isFile()&&headFile.getAbsolutePath().endsWith(".h")){
            parseHeader(headPath);
        }
        System.out.println();
    }

}
